﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Nirvana
{
    /// <summary>
    ///  UI下拉栏事件
    //// lua代码使用，代替OnValueChanged手动指定回调事件功能
    /// </summary>
    [RequireComponent(typeof(Dropdown))]
    [AddComponentMenu("Nirvana/UI/Bind/Event Bind Dropdown")]
    public sealed class UIEventBindDropdown : UIEventBind
    {
        [SerializeField]
        [EventName]
        private string eventName;

        private Dropdown _dropdown;
        private SignalEvent _signalevent;

        protected override void Awake()
        {
            base.Awake();
            this._dropdown = base.GetComponent<Dropdown>();
        }
        private void OnEnable()
        {
            this._dropdown.onValueChanged.AddListener(this.OnValueChanged);
        }

        private void OnDisable()
        {
            this._dropdown.onValueChanged.RemoveListener(this.OnValueChanged);
        }
        private void OnValueChanged(int index)
        {
            SignalEvent _event = this.FindSignalEvent();
            if (_event != null)
            {
                _event.Excute(index);
            }
        }

        private SignalEvent FindSignalEvent()
        {
            if (this._signalevent == null)
            {
                this._signalevent = base.GetSignalEvent(this.eventName);
            }
            return this._signalevent;
        }

        protected override void RefreshBind()
        {
            this._signalevent = base.GetSignalEvent(this.eventName);
        }


    }
}

